<?
include_class('band_members');
include_class('band_diaries');
include_class('m2');
if ($_GET['id']) {
	$bm = BandMember::get($_GET['id']);
	if ($_GET['media_instance_id'] > 0) { 
		$mi = MediaInstance::get($_GET['media_instance_id']);
		if (!db::isError($mi) && !db::isError($bm)) {
			if (($mi->getAreaID() == $bm->getAVAreaID() || $mi->getAreaID() == $bm->getPhotoAreaID()) && $bm->isActive()) {
				$view = 'media';
				$media = new SectionTemplate;
				$media->file = "media_detail.php";
				$media->args['media'] = $mi;
				$media->args['exitURL'] = (USE_MOD_REWRITE) ? SITE_WEB_DIRECTORY . '/members/' . $bm->getID() . '/' : SITE_WEB_DIRECTORY . '/band.php?id=' . $bm->getID();
			}
		}
	} else if ($_GET['diaries']) {
		$paging = new SectionTemplate;
		$paging->file = "paging.php";
		$paging->args['perPage'] = (is_numeric($_GET['e'])) ? $_GET['e'] : 10;
		$paging->args['start'] = (is_numeric($_GET['s'])) ? $_GET['s'] : $paging->args['start'] = 1;
		
		$view = 'diaries';
		$diaries = array();
		$result = $bm->getDiaries($_GET['e'], $_GET['s'], 1);
		$paging->args['total'] = $bm->getTotalDiaryEntries(1);
		$paging->args['url'] = (USE_MOD_REWRITE) ? SITE_WEB_DIRECTORY . 'members/' . $bm->getID() . '/diaries/' : '/band.php?id=' . $bm->getID() . '&diaries=1';
		foreach($result as $bnd) {
			$st = new SectionTemplate;
			$st->file = "band_diary_summary.php";
			$st->args['diary'] = $bnd;
			$diaries[] = $st;
		}
		$uo = $bm->getUserObject();
		$firstName = $uo->getFirstName();
		if (USE_MOD_REWRITE) {
			$bandDiaryLink = '<a href="' . SITE_WEB_DIRECTORY . '/members/' . $bm->getID() . '">' . $firstName . '</a>';
			$bandMembersLink = '<a href="' . SITE_WEB_DIRECTORY . '/members/">Return to Band Members</a>';
		} else {
			$bandDiaryLink = '<a href="' . SITE_WEB_DIRECTORY . '/band.php?id=' . $bm->getID() . '">' . $firstName . '</a>';
			$bandMembersLink = '<a href="' . SITE_WEB_DIRECTORY . '/band.php">Return to Band Members</a>';
		}
	} else if ($_GET['diaryID']) { 
		$di = BandMemberDiary::get($_GET['diaryID']);
		if (!db::isError($di)) {
			if ($di->getMemberID() == $bm->getID()) {
				$uo = $bm->getUserObject();
				$firstName = $uo->getFirstName();
				if (USE_MOD_REWRITE) {
					$bandDiaryLink = '<a href="' . SITE_WEB_DIRECTORY . '/members/' . $bm->getID() . '">' . $firstName . '</a>';
					$bandMembersLink = '<a href="' . SITE_WEB_DIRECTORY . '/members/">Return to Band Members</a>';
				} else {
					$bandDiaryLink = '<a href="' . SITE_WEB_DIRECTORY . '/band.php?id=' . $bm->getID() . '">' . $firstName . '</a>';
					$bandMembersLink = '<a href="' . SITE_WEB_DIRECTORY . '/band.php">Return to Band Members</a>';
				}
				$view = 'diary-detail';
				$detail = new SectionTemplate;
				$detail->file = "band_diary_detail.php";
				$detail->args['diary'] = $di;
			}
		}		
	}
	
	if (!$view) {
		$view = 'detail';
		$detail = new SectionTemplate;
		$detail->file = "band_member_detail.php";
		$detail->args['member'] = $bm;
		if (USE_MOD_REWRITE) {
			$bandMembersLink = '<a href="' . SITE_WEB_DIRECTORY . '/members/">Return to Band Members</a>';
		} else {
			$bandMembersLink = '<a href="' . SITE_WEB_DIRECTORY . '/band.php">Return to Band Members</a>';
		}
	}
} else { 
	$members = array();
	$bmlist = BandMemberList::get_active_band_members();
	if (count($bmlist) > 0) {
		foreach($bmlist as $member) {
			$bm = new SectionTemplate;
			$bm->file = "band_member_summary.php";
			$bm->args['member'] = $member;
			$members[] = $bm;
		}
	} else {
		unset($members);
	}
}

$header = new SectionTemplate;
$header->file = "header.php";
$footer = new SectionTemplate;
$footer->file = "footer.php";

?>